package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.icu.text.SimpleDateFormat;
import android.media.AudioMetadata;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.preference.EditTextPreference;
import android.telephony.MbmsGroupCallSession;
import android.telephony.mbms.MbmsErrors;
import android.text.Editable;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.text.style.SuperscriptSpan;
import android.util.AndroidException;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.bottomnavigation.BottomNavigationItemView;

import org.w3c.dom.Text;

import java.nio.ByteOrder;
import java.text.Format;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.security.auth.login.LoginException;

public class MainActivity extends AppCompatActivity {

    private static  final String TAG=MainActivity.class.getSimpleName();
    private EditText editText;
    private EditText aa;


   /* @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //onCreate()方法恢复数据时，通常在setContentView()之前
        if (null !=savedInstanceState){
            String data=savedInstanceState.getString("save_key","");

            if (TextUtils.isEmpty(data)){
                Toast.makeText(this,"未获取到保存数据",Toast.LENGTH_SHORT).show();
            }
        }
        setContentView(R.layout.activity);*/
        /*init();*/

        /*Button button=findViewById(R.id.btn);
         editText=findViewById(R.id.hh_id);
         aa=findViewById(R.id.mima_id);
         button.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
                 String z=editText.getText().toString();
                 String m=aa.getText().toString();
                     Toast.makeText(MainActivity.this, "登录成功！",
                             Toast.LENGTH_SHORT).show();
                 Log.e(TAG, "onClick: "+z );
                 Log.e(TAG, "onClick: "+m );
             }
         });*/


        /*
        TextView t1 = (TextView) findViewById(R.id.txtOne);
        String s1 = "<font color='blue'><b>百度一下,你就知道~：</b></font><br>";
         */

        /*s1 += "<a  href = 'http://www.baidu.com'>百度</a>";
        t1.setText(Html.fromHtml(s1));
        t1.setMovementMethod(LinkMovementMethod.getInstance());

        //1.构建通过span定制的字符串
        SpannableStringBuilder ssb = new SpannableStringBuilder();
        ssb.append("二狗，你变了！");
        //2.设置定制颜色
        //color.parseColor()将#FARGB色值转化为16进制色值
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor("#FF0090FF"));
        //3.将定制颜色span给到span字符串
        ssb.setSpan(colorSpan, 0, 2, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        //4.通过Textview进行显示
        t1.setText(ssb);

        //可点击的Span
        //匿名内部类，相当于实现了继承了该抽象类的内部类(匿名)的对象
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                super.updateDrawState(ds);
                //可点击文字的颜色
                ds.setColor(Color.parseColor("#FF0090FF"));
            }

            @Override
            public void onClick(@NonNull View view) {
                Toast.makeText(MainActivity.this, "二狗被点击了！", Toast.LENGTH_SHORT).show();
            }
        };
        ssb.setSpan(clickableSpan, 0, 2, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        t1.setText(ssb);
        //设置高亮颜色为透明
        t1.setHighlightColor(Color.TRANSPARENT);
        //添加文字可点击
        t1.setMovementMethod(new LinkMovementMethod());


        //25dp转化为px
        float dp = 100;
        final float scale = getResources().getDisplayMetrics().density;
        //由25dp转化来的px
        int px = (int) (dp * scale + 0.5f);
        Drawable drawable = getResources().getDrawable(R.drawable.ic_baseline_mode_comment_24);
        drawable.setBounds(0, 0, px, px);
        ImageSpan imageSpan1 = new ImageSpan(drawable);
        ssb.setSpan(imageSpan1, ssb.length() - 1, ssb.length(),
                Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        t1.setText(ssb);


         //* private void  initView()
        {
            //通过id获取xml布局中的控件
            TextView textHello = findViewById(R.id.text_hello);
            //对应android:text属性
            textHello.setText("");
            //对应android:maxLines
            textHello.setMaxLines(1);

            Log.i(TAG, "Log info");
            Log.d(TAG, "Log debug");
            Log.w(TAG, "Log warn");
            Log.e(TAG, "Log error");

        }*/


    /*}
    private void init(){
        CheckBox c=findViewById(R.id.protocol);
        Switch Switch=findViewById(R.id.switch_login);
        editText=findViewById(R.id.hh_id);
        ImageView imageView=findViewById(R.id.asd_id);
        //对应xml标签中android:src属性，只可以设置资源id
        imageView.setImageResource(R.mipmap.ic_launcher);
        //Bitmap 位图
       // imageView.setImageBitmap();
        //获取当前checkbox和开关switch的状态
        boolean isChecked=c.isChecked();
        boolean isCheck=Switch.isChecked();
        String ss=editText.getText().toString();
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                //输入前
                Log.d(TAG, "输入前: "+"charSequence="+charSequence+" i="+i+" i1="+i1+" i2="+i2);
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                //输入中
                Log.d(TAG, "输入中: "+"charSequence="+charSequence+" i="+i+" i1="+i1+" i2="+i2);
            }

            @Override
            public void afterTextChanged(Editable editable) {
                //输入后
                Log.d(TAG, "输入后: "+"editable="+editable.toString());
            }
        });
        //监听checkbox和开关switch的选中状态
        c.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            //当用户勾选/取消时调用
            //ischecked用户操作的checkedbox的状态
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                Toast.makeText(MainActivity.this,isChecked?"已经勾选":"没有勾选",Toast.LENGTH_SHORT).show();
            }
        });
        Switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                Toast.makeText(MainActivity.this,isChecked?"已经开启":"已经关闭",Toast.LENGTH_SHORT).show();
            }
        });*/


    /*}
    @Override
    protected void onStop() {
        super.onStop();
        Log.e(TAG, "onStop: 哈哈" );
    }
    //保存状态

    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("save_key","需要保存的数据");
    }
    //恢复状态
    @Override
    protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        //恢复数据
        String string=savedInstanceState.getString("save_key","");
    }*/
    private String colorSelect;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       /* *//*initView();
        initList();*//*
        //initListitem();
        initMyAdapterList();

    }
    private void initMyAdapterList() {
        ListView listView = findViewById(R.id.list_view);
        List<ChatMagModle> data = new ArrayList<>();
        for (int i = 0; i < 20000; i++) {
            ChatMagModle mode = new ChatMagModle();
            mode.setA("陈撮" + i);
            mode.setB("杀了陈撮" + i);
            mode.setC(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));

            data.add(mode);
        }
        MyAdapter myAdapter = new MyAdapter(this);
        myAdapter.setData(data);

        listView.setAdapter(myAdapter);*/
    }

    /*private void initMyAdapterList(){
        ListView listView=findViewById(R.id.list_view);

        List<ChatMagModle> data =new ArrayList<>();
        for (int i=0;i<20;i++){
            ChatMagModle modle=new ChatMagModle();

            modle.setA("张三"+i);
            modle.setB("别忘了还钱！"+i);
            modle.setC(new SimpleDateFormat("yyyy-MM-dd  ")
                    .format(new Date()));
            data.add(modle);
        }
        MyAdapter adapter=new MyAdapter(this);
        adapter.setData(data);

        listView.setAdapter(adapter);
    }*/
  /* private void initListitem() {
        ListView listView = findViewById(R.id.list_view);
        //1.构建数据
        List<Map<String, Object>> datalist = new ArrayList<>();
        Map<String, Object> item = new HashMap<>();
        item.put("tou",R.mipmap.b);
        item.put("name","服务");
        datalist.add(item);
        Map<String, Object> item2 = new HashMap<>();
        item2.put("tou",R.mipmap.b1);
        item2.put("name","收藏");
        datalist.add(item2);
        Map<String, Object> item3 = new HashMap<>();
        item3.put("tou",R.mipmap.b2);
        item3.put("name","朋友圈");
        datalist.add(item3);
        Map<String, Object> item4 = new HashMap<>();
        item4.put("tou",R.mipmap.b3);
        item4.put("name","卡包");
        datalist.add(item4);
        Map<String, Object> item5 = new HashMap<>();
        item5.put("tou",R.mipmap.b4);
        item5.put("name","   表情");
        datalist.add(item5);
        Map<String, Object> item6 = new HashMap<>();
        item6.put("tou",R.mipmap.b6);
        item6.put("name","    设置");
        datalist.add(item6);
        SimpleAdapter adapter =
                new SimpleAdapter(this, datalist,
                        R.layout.list_item,
                        new String[]{"tou","name"},
                        new int[]{R.id.list_item_icon,R.id.list_item_name});

        // //4.将适配器赋值给ListView
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView,
                                    View view, int i, long l) {
                String name=(String)datalist.get(i).get("name");
                if(name.equals("服务")){
                    Intent intent=new Intent(
                            MainActivity.this,fuwuActivity2.class);
                    startActivity(intent);}
            }
        });
    }*/

      /* {
            Map<String, Object> item = new HashMap<>();
            item.put("user_icon", R.drawable.a1);
            item.put("user_name", "陈森" );
            item.put("last_msg", "那你说怎么办" );
            item.put("msg_time", new SimpleDateFormat("HH:mm").format(new Date()));
            datalist.add(item);

           Map<String, Object> item1 = new HashMap<>();
           item1.put("user_icon",R.mipmap.a2);
           item1.put("user_name", "王老板" );
           item1.put("last_msg", "到时候来找你" );
           item1.put("msg_time", new SimpleDateFormat("HH:mm").format(new Date()));
           datalist.add(item1);

           Map<String, Object> item2 = new HashMap<>();
           item2.put("user_icon",R.mipmap.a3);
           item2.put("user_name", "宝宝" );
           item2.put("last_msg", "哈哈哈，笑死" );
           item2.put("msg_time", new SimpleDateFormat("HH:mm").format(new Date()));
           datalist.add(item2);

           Map<String, Object> item3 = new HashMap<>();
           item3.put("user_icon",R.mipmap.a4);
           item3.put("user_name", "王霖" );
           item3.put("last_msg", "开下门" );
           item3.put("msg_time", new SimpleDateFormat("HH:mm").format(new Date()));
           datalist.add(item3);

           Map<String, Object> item4 = new HashMap<>();
           item4.put("user_icon",R.mipmap.a5);
           item4.put("user_name", "李开心" );
           item4.put("last_msg", "这个怎么样" );
           item4.put("msg_time", new SimpleDateFormat("HH:mm").format(new Date()));
           datalist.add(item4);

           Map<String, Object> item5 = new HashMap<>();
           item5.put("user_icon",R.mipmap.a6);
           item5.put("user_name", "微信游戏" );
           item5.put("last_msg", "新版将至 等你开黑" );
           item5.put("msg_time", new SimpleDateFormat("HH:mm").format(new Date()));
           datalist.add(item5);

           Map<String, Object> item6 = new HashMap<>();
           item6.put("user_icon",R.mipmap.a7);
           item6.put("user_name", "腾讯视频" );
           item6.put("last_msg", "大结局，一起陪你看！" );
           item6.put("msg_time", new SimpleDateFormat("HH:mm").format(new Date()));
           datalist.add(item6);

           Map<String, Object> item7 = new HashMap<>();
           item7.put("user_icon",R.mipmap.a8);
           item7.put("user_name", "腾讯充值" );
           item7.put("last_msg", "王者荣耀|张良*千筹问战超前售卖" );
           item7.put("msg_time", new SimpleDateFormat("HH:mm").format(new Date()));
           datalist.add(item7);

           Map<String, Object> item8 = new HashMap<>();
           item8.put("user_icon",R.mipmap.a9);
           item8.put("user_name", "青少年成长助手" );
           item8.put("last_msg", "新中国的最新政策" );
           item8.put("msg_time", new SimpleDateFormat("HH:mm").format(new Date()));
           datalist.add(item8);

           Map<String, Object> item9 = new HashMap<>();
           item9.put("user_icon",R.mipmap.a10);
           item9.put("user_name", "京东白条" );
           item9.put("last_msg", "交易提醒" );
           item9.put("msg_time", new SimpleDateFormat("HH:mm").format(new Date()));
           datalist.add(item9);

           Map<String, Object> item10 = new HashMap<>();
           item10.put("user_icon",R.mipmap.a11);
           item10.put("user_name", "MIUI" );
           item10.put("last_msg", "最新功能，等你体验" );
           item10.put("msg_time", new SimpleDateFormat("HH:mm").format(new Date()));
           datalist.add(item10);

        //2.构建条目布局 list_item.xml
        //3.构建适配器
        //参数4 根据列表List<map>中的map的数据key
        //参数5 列表条目布局中的控件id
        SimpleAdapter adapter = new SimpleAdapter(this, datalist,
                R.layout.list_item,
                new String[]{"user_icon","user_name"
                ,"last_msg", "msg_time"},
                new int[]{R.id.list_item_touxiang,R.id.list_item_name, R.id.list_item_last_msg, R.id.list_item_time});
        // //4.将适配器赋值给ListView
        listView.setAdapter(adapter);
       *//* //条目点击事件

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            //position 条目角标 与 数据List顺序相同 从B开始
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String userName = (String) datalist.get(position).get("user_name");

                Toast.makeText(MainActivity.this, "被点击的用户是：" + userName, Toast.LENGTH_SHORT).show();
            }
        });*//*
    }*/


        /*private void initList() {
            ListView listView = findViewById(R.id.list_view);
            //1.构建数据
            List<String> data = new ArrayList<>();
            for (int i = 0; i < 20; i++) {
                data.add("列表条目" + i);
            }
            //2.构建条目布局 spinner_item.xml
            //3.构建适配器
            ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.activity_spinnner, data);
            //4.将适配器赋值给ListView
            listView.setAdapter(adapter);
        }
        private void initView() {
            Spinner spinner = findViewById(R.id.spinner_test);
            //获取String array
            String[] colors = getResources().getStringArray(R.array.spinner_color);

            List<String> colorList = new ArrayList<>();

            colorList.add("请选择颜色");
            colorList.add("红色|red");
            colorList.add("橙色|orgn");
            colorList.add("黄色|yellow");
            colorList.add("绿色|green");
            colorList.add("蓝色|blue");
            colorList.add("紫色|purple");

            //数据由外部传入时，需要通过适配
            //数据仍未string.xml中的死数据，下拉列表条目发生样式变化
            //android.R.layout.simple_spinner_dropdown_item 系统下拉列表条目布局
            ArrayAdapter<String> adapter =
                    //参数1：Context
                    //参数2：条目布局
                    //参数3：数据列表
                    new ArrayAdapter<>(this, R.layout.activity_spinnner, colorList);
            //静态数据，更换条目布局
            // ArrayAdapter.createFromResource(this,R.array.spinner_color, android.R.layout.simple_spinner_dropdown_item);
            //将适配器赋给Spinner
            spinner.setAdapter(adapter);
            spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                //选中时调用
                //position 内容字符串数组的角标
                @Override
                public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
                    if (position == 0) {
                        colorSelect = " ";
                        return;

                    }
                    Toast.makeText(MainActivity.this, "已经选择：" + colors[position], Toast.LENGTH_SHORT).show();
                    colorSelect = colors[position];
                }

                //未选中调用
                @Override
                public void onNothingSelected(AdapterView<?> adapterView) {
                    colorSelect = " ";
                }
            });

        }*/
}
